#import <Cocoa/Cocoa.h>

#import "BWGrid.h"

@class BWStitch;
@class BWStitchArray;
@class BWStitchSelection;

extern NSString *BWStitchView_NewStitchNotification;
extern NSString *BWStitchView_NewStitchKey;

extern NSString *BWStitchView_StitchSelectedNotification;
extern NSString *BWStitchView_StitchUnselectedNotification;
extern NSString *BWStitchView_SelectedStitchKey;

extern NSString *BWStitchView_PrintAreaChangeNotification;
extern NSString *BWStitchView_Anchor1SquareKey;
extern NSString *BWStitchView_Anchor2SquareKey;


extern NSString *BWStitchView_StitchBendNotification;

typedef enum {
    BWStitchView_Selection,
    BWStitchView_Drawing,
    BWStitchView_PrintArea,

    BWStitchView_BendyControlPoint,

    // stay in selection mode, but forget rest of tracking events
    BWStitchView_None_ThenSelection 

} BWStitchView_TrackMode;


typedef enum {
    trackNone, trackStart, trackEnd
} BWStitchView_TrackSide;


@interface BWStitchView : NSView
{
    BWGrid *grid;
    BWStitchArray *stitches;

    id delegate;

    int labelCounter; // for stitches without custom labels

    BWGrid_Square trackSquare;
    BWGrid_SquarePart trackPart;

    BOOL trackingStitch;
    BWStitch *trackStitch;
    BWStitch *templateStitch;

    BWStitchView_TrackMode trackMode;
    BWStitchSelection *selectedStitches;
    
    NSPoint startClick;
    BWStitchView_TrackSide trackSide;

    BOOL isPrinting;
    BOOL displayPrintArea;
    BOOL trackingPrintArea;
    BWGrid_Square printAreaAnchor1;
    BWGrid_Square printAreaAnchor2;

    NSDictionary *textAttributes;

    // tracking stuff for the bendy control point
    float originalBendFactor;
    float originalBendDisplacement;
    NSPoint bendTrackStart;

    BOOL showBehindStitches;

    BOOL drawBendControls;
    BOOL drawSelectionHighlighting;
    BOOL highlightBorder;

    BOOL trackCenterOnly;
}

- (id) initForPrintingWithGrid: (BWGrid *) printGrid
                      stitches: (BWStitchArray *) printStitches
                     labelFont: (NSFont *) printLabelFont
              printAreaAnchor1: (BWGrid_Square) anchor1
                       anchor2: (BWGrid_Square) anchor2;

- (void) setDelegate: (id) delegate;
- (id) delegate;

- (void) setGrid: (BWGrid *) newGrid;
- (BWGrid *) grid;

- (void) setStitches: (BWStitchArray *) stitches;

- (void) setTrackingStitch: (BWStitch *) stitch;
- (BWStitch *) trackingStitch;

- (void) setTemplateStitch: (BWStitch *) stitch;
- (BWStitch *) templateStitch;

- (void) setTrackMode: (BWStitchView_TrackMode) trackMode;

- (void) setLabelFont: (NSFont *) font;

- (void) setPrintAreaAnchor1: (BWGrid_Square) anchor1
                 anchor2: (BWGrid_Square) anchor2;

- (void) setShowPrintableArea: (BOOL) onOff;

- (void) setShowBehindStitches: (BOOL) onOff;

- (void) setDrawBendControls: (BOOL) onOff;
- (void) setDrawSelectionHighlighting: (BOOL) onOff;

- (void) setHighlightBorder: (BOOL) onOff;

- (void) unselectAll;

- (void) setTrackCenterOnly: (BOOL) trackCenterOnly;

// !!! fallback to just an NSSet?
- (void) setSelectedStitchSet: (BWStitchSelection *) selectedStitches;

@end // BWStitchView


@interface NSObject (BWStitchViewDelegate)

- (void) stitchView: (BWStitchView *) view
 changedStartSquare: (BWGrid_Square) startSquare
         squarePart: (BWGrid_SquarePart) startPart
          endSquare: (BWGrid_Square) endSquare
         squarePart: (BWGrid_SquarePart) endPart
          forStitch: (BWStitch *) stitch;

- (void) stitchView: (BWStitchView *) view
  changedBendFactor: (float) bendFactor
   bendDisplacement: (float) bendDisplacement
          forStitch: (BWStitch *) stitch;

- (void) stitchView: (BWStitchView *) view
shiftStitchesHorizontal: (int) horizontal
           vertical: (int) vertical;

- (void) deleteSelectedStitchesFromStitchView: (BWStitchView *) view;

@end // BWStitchViewDelegate
